import React, { useState, useEffect } from 'react'
import axios from 'axios'
import { Cell,Divider,Switch } from 'react-vant';
import HeaderNav from '../../../../../components/ayq/HeaderNav'
import './index.less'

export default function Push() {
  let [pustlist,setPushlist] = useState([])

  const getpush = async ()=>{
    const {data} = await axios.get("http://localhost:3000/ayqpush")
    setPushlist(data.data)
  }

  const uppush = async (id,checked) => {
    const {data} = await axios.post("http://localhost:3000/ayquppush",{id,checked})
    if(data.code === 200){
      getpush()
    }
  }

  useEffect(()=>{
    getpush()
  },[])
  return (
    <div className='push'>
      <HeaderNav title="推送设置" />
      <div className='body'>
        <Divider style={{margin:0}}/>
        {
          pustlist.map(item=>(
            <Cell key={item._id} title={item.title} rightIcon={
              <Switch
                size={20}
                defaultChecked={item.push}
                activeColor="#55A034" 
                inactiveColor="#fff"
                onChange={(checked) => uppush(item._id,checked)}/>
              }
            />
          ))
        }
      </div>
    </div>
  )
}
